package ds.recursive;

public class Anagram {

	private char[] input;
	private int count;
	
	public Anagram(String input) {
		this.input = new char[input.length()];
		for(int i=0;i<input.length();i++)
		{
			this.input[i] = input.charAt(i);
		}
		count = 0;
	}
	
	public void doAnagram(){
		doAnagram(input.length);
	}
	
	private void doAnagram(int newSize){
		if(newSize==1)
			return;
		for(int i=0;i<newSize;i++)
		{
			doAnagram(newSize-1);
			if(newSize==2)
				displayWord();
			rotate(newSize);
		}
	}
	
	private void displayWord()
	{
		if(count<99)
			System.out.print(" ");
		if(count<9)
			System.out.print(" ");
		System.out.print(++count+":");
		for(int i=0;i<input.length;i++)
		{
			System.out.print(input[i]);
		}
		
		System.out.print("   ");
		
		if(count%6==0)
		{
			System.out.println();
		}
	}
	
	private void rotate(int newSize)
	{
		int position = input.length - newSize;
		char temp = input[position];
		int i;
		for(i=position;i<input.length-1;i++)
		{
			input[i] = input[i+1];
		}
		input[i]=temp;
	}
}

class AnagramApp{
	public static void main(String args[])
	{
		new Anagram("12345").doAnagram();
	}
}